import { Injectable, OnModuleInit } from '@nestjs/common';
import { ModuleRef } from '@nestjs/core';
import { EventEmitter2 } from '@nestjs/event-emitter';
import NoticeDto from 'src/system/dto/NoticeDto';
import EventTypeEnum from './enum/EventTypeEnum';
import BaseEvent from './event/BaseEvent';

/*
 * @Author: jingfuu@163.com
 * @Date: 2024-12-04 09:54:20
 * @LastEditTime: 2024-12-12 16:15:42
 * @Description: 发送指定用户系统通知工具类
 */
@Injectable()
class UserNoticeMsgUtil implements OnModuleInit {
  private static eventEmitter: EventEmitter2;
  constructor(private moduleRef: ModuleRef) {}

  onModuleInit() {
    const eventEmitterInstance = this.moduleRef.get(EventEmitter2, {
      strict: false,
    });
    if (eventEmitterInstance) {
      UserNoticeMsgUtil.eventEmitter = eventEmitterInstance;
    }
  }

  /**
   * 发送用户系统通知
   * @param notice 通知数据
   */
  static sendUserNoticeMsg(notice: NoticeDto) {
    notice.noticeType = '3';
    notice.status = '0';
    // 发送消息
    this.eventEmitter.emit(
      EventTypeEnum.BUSINESS_MESSAGE_NOTICE,
      BaseEvent.builder<NoticeDto>()
        .data(notice)
        .type(EventTypeEnum.BUSINESS_MESSAGE_NOTICE)
        .build(),
    );
  }
}
export default UserNoticeMsgUtil;
